package com.elitel.frame.web.jdp_common_dictionary.service;

import com.elitel.frame.web.common.idworker.IdWorker;
import com.elitel.frame.web.jdp_common_dictionary.dao.JdpCommonDictionaryMapper;
import com.elitel.frame.web.jdp_common_dictionary.entity.JdpCommonDictionary;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * JDP通用字典 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-06-10
 */
@Service
public class JdpCommonDictionaryServiceImpl implements IJdpCommonDictionaryService {
    @Resource
    JdpCommonDictionaryMapper jdpCommonDictionaryMapper;

    @Override
    public String insert(JdpCommonDictionary dictionary) {
        dictionary.setDictionaryId(IdWorker.createStrId());
        dictionary.setCreatedTime(new Date());
        int insert = jdpCommonDictionaryMapper.insert(dictionary);
        return insert > 0 ? dictionary.getDictionaryId() : "insert failed";
    }

    @Override
    public String update(JdpCommonDictionary dictionary) {
        int update = jdpCommonDictionaryMapper.update(dictionary);
        return update > 0 ? dictionary.getDictionaryId() : "update failed";
    }

    @Override
    public String deleteById(String id) {
        int delete = jdpCommonDictionaryMapper.deleteById(id);
        return delete > 0 ? id : "delete failed";
    }

    @Override
    public JdpCommonDictionary selectById(String id) {
        return jdpCommonDictionaryMapper.selectById(id);
    }

    @Override
    public Map<String, Object> selectAll(String name, Integer page, Integer rows, String categoryId) {
        if (page == null) page = 1;
        if (rows == null) rows = 10;
        PageHelper.startPage(page, rows);
        List<JdpCommonDictionary> list = jdpCommonDictionaryMapper.selectAll(name, categoryId);
        PageInfo<JdpCommonDictionary> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", pageInfo.getTotal());
        return map;
    }
}
